//
//  RestaruantsNearbyViewController.m
//  FoodFamily
//
//  Created by dlios on 15-6-20.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "RestaruantsNearbyViewController.h"

@interface RestaruantsNearbyViewController ()

@end

@implementation RestaruantsNearbyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建列表
    [[FoodCitySql sharDataHandel]creatTable1];
    
    //刷新
    self.nextPage = 1;

    
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
   self.navigationItem.title = @"附近餐厅";
    
    self.MyTableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-20 - 44 - 49) style:UITableViewStylePlain];
    self.MyTableV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.MyTableV];
    self.MyTableV.delegate = self;
    self.MyTableV.dataSource = self;
    [self.MyTableV release];
    
//    self.MyTableV.separatorStyle =UITableViewCellSeparatorStyleNone;
     self.array = [NSMutableArray array];
    [self getInFo];
    
    self.MBHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.MBHUD.labelText = @"正在加载中...";
    [self.MBHUD show:YES];
    //下来刷新  上拉加载
    [self.MyTableV.header beginRefreshing];
    
    [self headerRefesh];
    [self footerRefesh];
    // Do any additional setup after loading the view.
}

#warning   上拉刷新
-(void)headerRefesh {
    self.MyTableV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.nextPage = 1;
        self.downUpdata = YES;
        
        //让菊花旋转起来
        self.MBHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.MBHUD.labelText = @"正在加载中...";
        [self.MBHUD show:YES];
        [self getInFo];
        
        // 刷新表格
        [self.MyTableV reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        
        
        
    }];
    
}
#warning 下拉加载
-(void)footerRefesh {
    
    
    
    
    self.MyTableV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        
        
        self.nextPage++;
        self.downUpdata = NO;
        self.MBHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.MBHUD.labelText = @"正在加载中...";
        [self.MBHUD show:YES];
        [self getInFo];
        
        // 刷新表格
        [self.MyTableV reloadData];
        
    }];
    
}


-(void)getInFo{
    if (self.nextPage == 1) {
        
        self.array = [[FoodCitySql sharDataHandel]selectAllNearbyResturant];
    }

       NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/search/nearby/recommandplace/lng=%f&lat=%f&page=%ld&session_id=00001227993a5661b25da19c79c2a7c9a6affc&distance=1000&food_category_name=&price=0~200000",self.lng,self.lat,self.nextPage];
    [AFNGetDataValue getDataAFN:url block:^(NSMutableDictionary *dataDic) {

        if (dataDic == nil) {
            [self.MyTableV.footer endRefreshing];
            [self.MyTableV.header endRefreshing];
            [self.MBHUD hide:YES];
        }
        else{
            if ([[dataDic objectForKey:@"result_count"] integerValue] == 0) {
                [self.MyTableV.footer endRefreshing];
                [self.MyTableV.header endRefreshing];
                [self.MBHUD hide:YES];
                UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
                view.backgroundColor = [UIColor whiteColor];
                [self.view addSubview:view];
                
                UIAlertView *alView = [[UIAlertView alloc]initWithTitle:nil message:@"您的附近暂无推荐菜" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alView show];
                return;
            }
        
            
            
            if (self.nextPage == 1) {
                [[FoodCitySql sharDataHandel]dropTable1];
                [[FoodCitySql sharDataHandel]creatTable1];

        }
            if (self.downUpdata) {
                
                [self.array removeAllObjects];
            }
            
            
        NSMutableArray *array = [dataDic objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            FoodCityModle *FoodCM = [[FoodCityModle alloc]init];
            FoodCM.category = [dic objectForKey:@"category"];
            FoodCM.phone = [dic objectForKey:@"phone"];
            FoodCM.place_id = [dic objectForKey:@"place_id"];
            FoodCM.distance = [dic objectForKey:@"distance"];
            FoodCM.address1 = [dic objectForKey:@"address"];
            FoodCM.category_id = [dic objectForKey:@"category_id"];
            FoodCM.place_name1 = [dic objectForKey:@"place_name"];
            FoodCM.star = [dic objectForKey:@"star"];
            
           [self.array addObject:FoodCM];
            
            if (self.nextPage == 1) {
                [[FoodCitySql sharDataHandel]insertNearbyResturantFoodCityModel:FoodCM];
                
            }
            [FoodCM release];
                   }
            [self.MyTableV reloadData];
            [self.MyTableV.footer endRefreshing];
            [self.MyTableV.header endRefreshing];
            [self.MBHUD hide:YES];

        }
        
    }];
   

    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    [self.navigationController popViewControllerAnimated:YES];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
   static NSString *indentCell = @"cell";
   RsetaruantsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentCell];
    if (cell == nil) {
        cell = [[RsetaruantsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentCell];
        
    }
    
    FoodCityModle *foodCM = [self.array objectAtIndex:indexPath.row];
    cell.foodCM =foodCM;

   
   
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return  HEIGHT/4;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RestNearbyViewController *restDVC = [[RestNearbyViewController alloc]init];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.navigationController pushViewController:restDVC animated:YES];
    FoodCityModle *foodCM = [self.array objectAtIndex:indexPath.row];
    restDVC.place_id =foodCM.place_id;
       

    
    [restDVC release];

}



-(void)leftBarButtonAction{

    [self.navigationController popToRootViewControllerAnimated:YES];

}
-(void)dealloc{
    [_MyTableV release];
    [_array release];
    [_MBHUD release];
    [super dealloc];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
